class UserAccountController {

  def mailService

  def index = { redirect(action: list, params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [userAccountInstanceList: UserAccount.list(params), userAccountInstanceTotal: UserAccount.count()]
  }

  def show = {
    def userAccountInstance = UserAccount.get(params.id)
    if (!userAccountInstance) {
      flash.message = "UserAccount not found with id ${params.id}"
      redirect(action: list)
    }
    else { return [userAccountInstance: userAccountInstance] }
  }

//  def delete = {
//    def userAccountInstance = UserAccount.get(params.id)
//    if (userAccountInstance) {
//      try {
//        userAccountInstance.delete()
//        flash.message = "UserAccount ${params.id} deleted"
//        redirect(action: list)
//      }
//      catch (org.springframework.dao.DataIntegrityViolationException e) {
//        flash.message = "UserAccount ${params.id} could not be deleted"
//        redirect(action: show, id: params.id)
//      }
//    }
//    else {
//      flash.message = "UserAccount not found with id ${params.id}"
//      redirect(action: list)
//    }
//  }

  def edit = {
    def userAccountInstance = UserAccount.get(params.id)

    if (!userAccountInstance) {
      flash.message = "UserAccount not found with id ${params.id}"
      redirect(action: list)
    }
    else {
      return [userAccountInstance: userAccountInstance]
    }
  }

  def update = {
    def userAccountInstance = UserAccount.get(params.id)
    if (userAccountInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (userAccountInstance.version > version) {

          userAccountInstance.errors.rejectValue("version", "userAccount.optimistic.locking.failure", "Another user has updated this UserAccount while you were editing.")
          render(view: 'edit', model: [userAccountInstance: userAccountInstance])
          return
        }
      }
      userAccountInstance.properties = params
      if (!userAccountInstance.hasErrors() && userAccountInstance.save()) {
        flash.message = "UserAccount ${params.id} updated"
        redirect(action: show, id: userAccountInstance.id)
      }
      else {
        render(view: 'edit', model: [userAccountInstance: userAccountInstance])
      }
    }
    else {
      flash.message = "UserAccount not found with id ${params.id}"
      redirect(action: edit, id: params.id)
    }
  }

  def create = {
    def userAccountInstance = new UserAccount()
    userAccountInstance.properties = params
    return ['userAccountInstance': userAccountInstance]
  }

  //TODO Part of alternative mailService
  //def notifierService

  def save = {
    def userAccountInstance = new UserAccount(params)
    if (!userAccountInstance.hasErrors() && userAccountInstance.save()) {
      // Send weclome email

      //TODO Alternative Way to do mailService, but generates same error as in UserAccountControllerUnitTest
      //notifierService.contactUser(userAccountInstance)

      mailService.sendMail {
        to "${userAccountInstance.email}"
        subject "Welcome ${userAccountInstance?.firstName} ${userAccountInstance?.lastName}"
        html g.render(template: "/common/welcomeEmail", model: [user: userAccountInstance])
      }

      flash.message = "UserAccount ${userAccountInstance.email} created"
      redirect(controller: 'login', action: 'login', id: userAccountInstance.email)
    }
    else {
      render(view: 'create', model: [userAccountInstance: userAccountInstance])
    }
  }
}
